package com.platform.mobile.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.mobile.bean.Dept;
import com.platform.mobile.bean.DeptBean;
import com.platform.mobile.bean.Doctor;
import com.platform.mobile.bean.DoctorBean;
import com.platform.mobile.bean.LoginAssitantInfo;
import com.platform.mobile.dao.DatasourceDao;
import com.platform.mobile.entity.VisitOrg;
import com.platform.mobile.entity.VisitUser;
import com.platform.mobile.service.DatasourceService;
import com.platform.mobile.session.SessionManager;
import com.platform.tool.HelpFunctions;

@Service
public class DatasourceServiceImpl implements DatasourceService{

	@Autowired
	private DatasourceDao dao;
	
	@Override
	public DeptBean getDeptDatasource(String sessionID) {
		
		DeptBean deptBean = new DeptBean();
		
		Object obj 
			= SessionManager.getInstance().getSessionInfo(sessionID);
		if(obj == null) return null;
		
		LoginAssitantInfo info = (LoginAssitantInfo) obj;
		Integer org_id = info.getOrg_id();
		
		if(org_id == null || org_id == -1) return null;
/*		List<VisitOrg> orgList = dao.getOrgById(org_id);
		if(HelpFunctions.isEmpty(orgList)) return null;
		
		Integer parent_id = orgList.get(0).getParent_id();*/
		List<VisitOrg> deptList = dao.getOrgByParentID(org_id);
		if(HelpFunctions.isEmpty(deptList)) return null;
		
		for(VisitOrg org : deptList){
			Dept dept = new Dept();
			dept.setId(org.getID());
			dept.setName(org.getORG_NAME());
			deptBean.getDpet_list().add(dept);
		}
		
		return deptBean;
	}

	@Override
	public DoctorBean getDoctorDatasource(Integer org_id) {
		
		DoctorBean doctorBean = new DoctorBean();
		
		List<VisitUser> doctor_list 
				= dao.getUserByDeptID(org_id);
		if(HelpFunctions.isEmpty(doctor_list)) return null;
		
		for(VisitUser user : doctor_list){
			Doctor doctor = new Doctor();
			doctor.setID(user.getId());
			doctor.setName(user.getReal_name());
			
			doctorBean.getDoctor_list().add(doctor);
		}
		return doctorBean;
	}
	
}
